import cv2
import numpy as np
from skimage.color import rgb2gray
from skimage.feature import canny

sdrLen = 1
imgChannel = 3
rgbSpaceNum = 256
encodingNumPerChannel = rgbSpaceNum - 1 + sdrLen
encodingLength = sdrLen * imgChannel
encodingSpace = imgChannel * encodingNumPerChannel


class Utils:

    @staticmethod
    def encodeRGB(rgb):
        result = np.array([], dtype=int)
        for i, c in enumerate(rgb):
            ccode = [c + j + encodingNumPerChannel * i for j in range(sdrLen)]
            result = np.concatenate([result, ccode])
        return result

    @staticmethod
    def getEdgeImg(origImg):
        grayImg = rgb2gray(origImg)
        edges = canny(grayImg)
        return edges

    @staticmethod
    def encodeScenarioUsingImage(originalImage, edgeImage):
        edgePoints = np.where(edgeImage == True)
        scenarioObject = {
            "name": "scene1",
            "features": []
        }
        for h, w in zip(edgePoints[0], edgePoints[1]):
            scenarioObject["features"].append({
                "top": h,
                "left": w,
                "width": 1,
                "height": 1,
                "featureValue": Utils.encodeRGB(originalImage[h, w])
            })
        return scenarioObject


